package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author kk
 */
@Slf4j
@Service
@Transactional
public class ArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;


    /**
     * 根据参数加载文章列表
     *
     * @param dto
     * @param loadtype
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short loadtype) {
        //对数据进行校验
        if (loadtype == null) {
            loadtype = 1;
        }
        if (dto.getSize() == null || dto.getSize() == 0) {
            dto.setSize(10);
        }

        dto.setSize(Math.min(dto.getSize(), 30));

        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag("__all__");
        }
        if (dto.getMinBehotTime() == null && dto.getMaxBehotTime() == null)
            dto.setMinBehotTime(new Date(0));
        dto.setMaxBehotTime(new Date());

        //查询数据
        List<ApArticle> list = apArticleMapper.load(dto, loadtype);

        return ResponseResult.okResult(list);
    }

    /**
     * 保存app端相关文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //校验参数
        if (dto == null || StringUtils.isEmpty(dto.getContent())) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断文章的id是否存在
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        if (dto.getId() == null) {//不存在就新增
            save(apArticle);
            ApArticleConfig config = new ApArticleConfig();
            config.setArticleId(apArticle.getId());
            config.setIsDown(false);
            config.setIsComment(true);
            config.setIsDelete(false);
            config.setIsForward(true);

            apArticleConfigMapper.insert(config);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setArticleId(apArticle.getId());
            articleContentMapper.insert(apArticleContent);
        } else {//存在进行修改
            updateById(apArticle);
            ApArticleContent apArticleContent = articleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());
            articleContentMapper.updateById(apArticleContent);
        }
        return ResponseResult.okResult(apArticle.getId());
    }
}
